<{include file="public/header.html"}>
<script language="javascript">
<!--
Ext.QuickTips.init(); 

if (Ext.form.ComboBox) {
    Ext.apply(Ext.form.ComboBox.prototype, {
        triggerAction:'all',
        selectOnFocus:true,
        editable:false,
        mode:'local',
        lazyInit:false,
        emptyText:'',
        displayField:'sText',
        anchor:"95%"
    });
}
if (Ext.form.TimeField) {
    Ext.apply(Ext.form.TimeField.prototype, {
        format:'H:i',
        emptyText:'',
        increment:10,
        maxLength:5,
        anchor:"",
        width:80
   });
}
var projForm = new Ext.FormPanel({
    labelAlign:'right',
    frame :true,
    layout:'form',
    region:'center',
    title :'项目信息',
    reader:Ext.data.JsonReader({
        successProperty:"success",
        totalProperty:"total", 
        id: "strProjectID" 
        }, ['strProjectName','strProjectCode','strStartDate','strPriority',
            'strProjectType','strFinishDate','strManager','strContent',
            'strWorkDay','strAMStartTime','strPMStartTime','strIsPersonal','strAMEndTime',
            'strPMEndTime','strCreateInfo'] 
        ),

    items: [{xtype:"hidden",name:"strProjectID",id:'sProjectID',value:''},
       {xtype:'textfield',fieldLabel:'项目名称',name:'strProjectName',anchor:'96%',vtype:'dblCharField',
        maxLength:250,allowBlank:false
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:0.5,
            layout:'form',
            items :[{xtype:"textfield", fieldLabel:"任务编号", name:"strProjectCode", 
                     maxLength:50,anchor:'96%'},
                    {xtype:"datefield",fieldLabel:'项目开始日期',name:'strStartDate'},
                    {xtype:"combo",hiddenName:"strPriority",fieldLabel:"优先级",forceSelection:true,
                    value:'2',valueField:'sValue',
                    store:new Ext.data.SimpleStore({
                        data:<{ $aImportance }>,
                        fields:['sValue','sText']
                    })
                    }]
        },{
            columnWidth:0.5,
            layout:'form',
            items:[{xtype:"textfield",ame:"strProjectType",fieldLabel:"项目类别",
                maxLength:30,anchor:'96%'},
                {xtype:"datefield",fieldLabel:'项目结束日期',name:'strFinishDate'},
                {xtype:"combo",hiddenName:"strManager",fieldLabel:"项目负责人",
                valueField:'sValue',value:'<{ $sDefaultManager }>',
                store:new Ext.data.SimpleStore({
                    data  :<{ $aManager }>,
                    fields:['sValue','sText']
                }) }]
        }]
    },{
        xtype:'htmleditor',name:'strContent',fieldLabel:'项目情况说明',
        height:120,anchor:'98%'
    },{
        xtype:"panel",
        layout:"column",
        isFormField:true,
        fieldLabel:"工作日",
        items:[{xtype:"hidden",name:"strWorkDay",id:'sWorkDay',value:'<{$aDefaultWeek}>'},
          {
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期日',name:"strWeek0",id:"sWeek0"
        },{
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期一',name:"strWeek1",id:"sWeek1"
        },{
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期二',name:"strWeek2",id:"sWeek2"
        },{
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期三',name:"strWeek3",id:"sWeek3"
        },{
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期四',name:"strWeek4",id:"sWeek4"
        },{
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期五',name:"strWeek5",id:"sWeek5"
        },{
            columnWidth:0.14,
            xtype:'checkbox',boxLabel:'星期六',name:"strWeek6",id:"sWeek6"
        }]
    },{
        xtype:"panel",
        layout:"column",
        anchor:'100%',
        items:[{
            columnWidth:0.5,
            layout:'form',
            items :[{xtype:"timefield",fieldLabel:'上午上班时间',name:'strAMStartTime',
                value:'<{ $aDefaultTime.AM_START_TIME }>'},
                {xtype:"timefield",fieldLabel:'下午上班时间',name:'strPMStartTime',
                value:'<{ $aDefaultTime.PM_START_TIME }>'},
                {xtype:"combo",hiddenName:"strIsPersonal",fieldLabel:"开放程度",forceSelection:true,
                valueField:'sValue',value:'<{ $sSECURITY }>',
                store:new Ext.data.SimpleStore({
                    data  :<{ $aSECURITY }>,
                    fields:['sValue','sText']
                }) }]
        },{
            columnWidth:0.5,
            layout:'form',
            items :[{xtype:"timefield",fieldLabel:'上午下班时间',name:'strAMEndTime',
                    value:'<{ $aDefaultTime.AM_END_TIME }>'},
                {xtype:"timefield",fieldLabel:'下午下班时间',name:'strPMEndTime',
                    value:'<{ $aDefaultTime.PM_END_TIME }>'},
                {xtype:"textfield",fieldLabel:"创建者和时间",name:"strCreateInfo",
                    readOnly:true,anchor:'98%'}]
        }]
    }],

    buttons: [{
            text:"删 除",id:'btnDel',disabled:true,handler:doDelTask
        },{
            text: '保 存',id:'btnSave',handler:doSaveTask 
        },{
            text: '取 消',id:'btnCanel',handler:function() { }
        }]
});

var viewport = new Ext.Viewport({
    layout:'border',
    items : [projForm]
});

function getWorkDay(sWorkDay) {
    return (Ext.getCmp(sWorkDay).checked ? "1" : "0");
}
function setWorkDay(sWorkday) {
    for (var i=0; i<sWorkday.length; i++) {
        Ext.getCmp("sWeek"+i).setValue(sWorkday.charAt(i)=="1"?true:false);
    }
}
function showProject(projectID) {
    projForm.getForm().load({
        url:'./projectAjax.php', 
        //waitMsg:'正在载入数据...', 
        params:{ strProjectID:projectID,strAction:ACTION_VIEW},

        success : function(form, action){
            Ext.getCmp("sProjectID").setValue(projectID);
            if (action.result.data.strWorkDay != "") {
                setWorkDay(action.result.data.strWorkDay);
            }
            Ext.getCmp("btnDel").setDisabled(false);
        },
        failure : function(form, action){
            Ext.MessageBox.alert('错误', '数据加载失败！');
        }
    });
}
//setWorkDay('<{$aDefaultWeek}>');
showProject('<{$sProjectID}>');

function checkForm() {
    //判断时间是否合法的
    var a = Ext.get("strAMStartTime").getValue();     //上午上班时间
    var b = Ext.get("strAMEndTime").getValue();       //上午下班时间
    var c = Ext.get("strPMStartTime").getValue();     //下午上班时间
    var d = Ext.get("strPMEndTime").getValue();       //下午下班时间

    if (a > b) {
        Ext.Msg.alert("错误","上午下班时间比上午上班时间早，请修正");
        return false;
    }
    if (c > d) {
        Ext.Msg.alert("错误","下午下班时间比下午上班时间早，请修正");
        return false;
    }
    if (b > c) {
        Ext.Msg.alert("错误","下午上班时间比上午下班时间早，请修正");
        return false;
    }

    var sWorkday = getWorkDay("sWeek0")+
                   getWorkDay("sWeek1")+getWorkDay("sWeek2")+
                   getWorkDay("sWeek3")+getWorkDay("sWeek4")+
                   getWorkDay("sWeek5")+getWorkDay("sWeek6");
    if (sWorkday == "0000000") {
        Ext.Msg.alert("数据错误","必须至少选择一个工作日");
        return false;
    } else {
        Ext.getCmp("sWorkDay").setValue(sWorkday);
    }

    return true;
}
function doSaveTask() {
    var ff = projForm.getForm();
    if (ff.isValid()) {
        if (!checkForm()) return false;
        var sAction = (Ext.getCmp("sProjectID").getValue()=="" ? ACTION_INSERT : ACTION_UPDATE);
        ff.submit({
            url     : './projectAjax.php',
            method  : 'POST',
            //waitMsg : '正在保存数据...',
            params  : {strAction:sAction},
            success : function(form, action){
                Ext.MessageBox.alert('提示', action.result.msg);
                if (sAction == ACTION_INSERT) {
                    Ext.getCmp("sProjectID").setValue(action.result.sProjectID);
                }
            },
            failure : function(form, action) {
                //Ext.MessageBox.alert('错误', action.response.responseText);
                Ext.MessageBox.alert('错误', '数据保存失败！');
            }
        });
    } else { //form checked is invalid
        Ext.MessageBox.alert('错误',"填写的数据有错误，请检查");
    }
};
function doDelTask() {
    var sProjectID = Ext.getCmp("sProjectID").getValue();
    if (sProjectID != "") {
        Ext.MessageBox.confirm('确认信息',"该项目下的所有相关信息将会被一起删除，是否继续？",function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url : './projectAjax.php',
                    params : {strAction:ACTION_DELETE,strProjectID:sProjectID},
                    success: function() {
                        Ext.MessageBox.alert('删除项目',"项目删除成功");},
                    failure: function(form,action) {
                        Ext.MessageBox.alert('删除项目',action.result.msg);
                    }
                });
            }
        });
    }
}

//-->
</script>

<{include file="public/footer.html"}>